using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectThongTinWebsite()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINWEBSITE
			};
			return View(CreateViewModel(data));
		}



		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteThongTinWebsite(int id)
		{
			var deleteItem = _repThongTinWebsite.SelectByID(id);
			var fileName = deleteItem.Banner;
			var result = _repThongTinWebsite.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repThongTinWebsite.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfThongTinWebsite(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINWEBSITE,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateThongTinWebsite(FormCollection forms)
		{
			string id = forms.GetValues("ThongTinWebsite_ID")[0];
			string namphattrien = forms.GetValues("ThongTinWebsite_NamPhatTrien")[0];
			string tencongty = forms.GetValues("ThongTinWebsite_TenCongTy")[0];
			string diachi = forms.GetValues("ThongTinWebsite_DiaChi")[0];
			string dienthoai = forms.GetValues("ThongTinWebsite_DienThoai")[0];
			string hotline = forms.GetValues("ThongTinWebsite_Hotline")[0];
			string emailcongty = forms.GetValues("ThongTinWebsite_EmailCongTy")[0];
			string matkhauemailcongty = forms.GetValues("ThongTinWebsite_MatKhauEmailCongTy")[0];
			string gioithieu = forms.GetValues("ThongTinWebsite_GioiThieu")[0];
            string loiichkhachhang = forms.GetValues("ThongTinWebsite_LoiIchKhachHang")[0];
            string gioithieurutgon = forms.GetValues("ThongTinWebsite_GioiThieuRutGon")[0];
            string loiichkhachhangrutgon = forms.GetValues("ThongTinWebsite_LoiIchKhachHangRutGon")[0];
            string diachidanang = forms.GetValues("ThongTinWebsite_DiaChiDaNang")[0];
            string dienthoaidanang = forms.GetValues("ThongTinWebsite_DienThoaiDaNang")[0];
            string emaildanang = forms.GetValues("ThongTinWebsite_EmailDaNang")[0];

			var file = Request.Files["ThongTinWebsite_Banner"];
			string refName = "";
			var editItem = _repThongTinWebsite.SelectByID(int.Parse(id));
			
            editItem.NamPhatTrien = namphattrien;
			editItem.TenCongTy = tencongty;
			editItem.DiaChi = diachi;
			editItem.DienThoai = dienthoai;
			editItem.Hotline = hotline;
			editItem.EmailCongTy = emailcongty;
            if (editItem.MatKhauEmailCongTy != matkhauemailcongty)
            {
                editItem.MatKhauEmailCongTy = Encryption.Encrypt(matkhauemailcongty);
            }
			editItem.GioiThieu = gioithieu;
            editItem.LoiIchKhachHang = loiichkhachhang;
            editItem.GioiThieuRutGon = StringHelper.Truncate(gioithieurutgon, WebConfiguration.ThongTinWebsiteSortDescription);
            editItem.LoiIchKhachHangRutGon = StringHelper.Truncate(loiichkhachhangrutgon, WebConfiguration.ThongTinWebsiteSortDescription);
            editItem.DiaChiDaNang = diachidanang;
            editItem.EmailDaNang = emaildanang;
            editItem.DienThoaiDaNang = dienthoaidanang;

			var result = false;
			if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Home/"), id, editItem.Banner, ref refName);
				editItem.Banner = refName;
			}
			result = _repThongTinWebsite.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINWEBSITE,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfThongTinWebsite", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertThongTinWebsite(FormCollection forms)
		{
			string namphattrien = forms.GetValues("ThongTinWebsite_NamPhatTrien")[0];
			string tencongty = forms.GetValues("ThongTinWebsite_TenCongTy")[0];
			string diachi = forms.GetValues("ThongTinWebsite_DiaChi")[0];
			string dienthoai = forms.GetValues("ThongTinWebsite_DienThoai")[0];
			string hotline = forms.GetValues("ThongTinWebsite_Hotline")[0];
			string emailcongty = forms.GetValues("ThongTinWebsite_EmailCongTy")[0];
			string matkhauemailcongty = forms.GetValues("ThongTinWebsite_MatKhauEmailCongTy")[0];
			string gioithieu = forms.GetValues("ThongTinWebsite_GioiThieu")[0];
            string loiichkhachhang = forms.GetValues("ThongTinWebsite_LoiIchKhachHang")[0];
            string gioithieurutgon = forms.GetValues("ThongTinWebsite_GioiThieuRutGon")[0];
            string loiichkhachhangrutgon = forms.GetValues("ThongTinWebsite_LoiIchKhachHangRutGon")[0];
            string diachidanang = forms.GetValues("ThongTinWebsite_DiaChiDaNang")[0];
            string dienthoaidanang = forms.GetValues("ThongTinWebsite_DienThoaiDaNang")[0];
            string emaildanang = forms.GetValues("ThongTinWebsite_EmailDaNang")[0];

			var file = Request.Files["ThongTinWebsite_Banner"];
			string refName = "";
			var newItem = new ThongTinWebsite
			{
				NamPhatTrien = namphattrien,
				TenCongTy = tencongty,
				DiaChi = diachi,
				DienThoai = dienthoai,
				Hotline = hotline,
				EmailCongTy = emailcongty,
				MatKhauEmailCongTy = Encryption.Encrypt(matkhauemailcongty),
                Banner = "noimages.jpg",
				GioiThieu = gioithieu,
                LoiIchKhachHang = loiichkhachhang,
                GioiThieuRutGon = StringHelper.Truncate(gioithieurutgon, WebConfiguration.ThongTinWebsiteSortDescription),
                LoiIchKhachHangRutGon = StringHelper.Truncate(loiichkhachhangrutgon, WebConfiguration.ThongTinWebsiteSortDescription),
                DiaChiDaNang = diachidanang,
                DienThoaiDaNang = dienthoaidanang,
                EmailDaNang = emaildanang
			};
			var result = _repThongTinWebsite.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Home/"), newItem.ID.ToString(), string.Empty, ref refName);
					if (result)
					{
						var addedItem = _repThongTinWebsite.SelectByID(newItem.ID);
						addedItem.Banner = refName;
						_repThongTinWebsite.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINWEBSITE,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectThongTinWebsite", CreateViewModel(data));
		}
	}
}